/**
 * ThreedHelper
 */

import "babel-polyfill";

class ThreedHelper {
    constructor() {
        // global texts
        this.TEXTS = {
            GENERATION: $._('The model generation...'),
            CHECK_FILE: $._('Please check model file'),
            FILE_ERROR: $._('the model file is error'),
            GENERATED: $._('Models have been generated'),
            PREVIEW: $._("Please use the mobile phone Preview"),
            GENERATE_ERROR: $._('The model generates an error')
        };
        // loop interval
        this.interval = 5000;
        // loop data
        this.threeds = new Map();
        // require dependencies
        this.consts = null;
        this.requestState = null;
        this.state = 0;
        this.resetWidget = null;
    }
    pushThreed(threed) {
        this.threeds.set(threed.id, $.extend(true, {}, threed));
    }
    remove(id) {
        const threed = this.getThreedById(id);
        if (threed) {
            this.endLoop(threed);
            this.threeds.delete(id);

            this.checkEndAllLoop();
        }
    }
    endAllLoop() {
        for (let threed of this.threeds.values()) {
            this.endLoop(threed);
        }
    }
    endLoop(threed) {
        if (threed.timer) {
            clearTimeout(threed.timer);
            threed.timer = null;
        }
        if (threed.xhr) {
            threed.xhr.abort();
        }
    }
    checkEndAllLoop() {
        const length = this.getThreedLength();
        if (length === 0) {
            this.endAllLoop();
        }
    }
    getThreedLength() {
        return this.threeds.size;
    }
    getThreedById(id) {
        return this.threeds.get(id);
    }
    updateMsg(id, m1, m2) {
        const threed = this.getThreedById(id);
        if (threed) {
            threed.msg1 = m1;
            threed.msg2 = m2;
            threed.propertyChanged = true;
            // this.root.updateLocaleWidget(threed);
            this.resetWidget && this.resetWidget(threed);
            // this.eventHelper.trigger(this.consts.EVENTS.THREED_WIDGET_PROPERTY_CHANGED, threed);
        }
    }
    updateThreed(id, data) {
        let key, value;
        const select_material = {},
            select_animation = -1;

        const threed = this.getThreedById(id);
        if (threed) {
            delete data.state;
            if (data.u3d_materials && !threed.select_material) {
                for (let u3d of data.u3d_materials.values()) {
                    key = u3d[0];
                    value = u3d[1][0];

                    select_material[key] = value;
                }
                data.select_material = select_material;
            }

            if (!data.u3d_materials) {
                delete threed.select_material;
                delete threed.u3d_materials;
            }

            if (!data.u3d_animations) {
                delete threed.u3d_animations;
            }

            // 默认勾选全部动作
            if (threed.select_animation == undefined) {
                data.select_animation = select_animation;
            }
            return $.extend(true, {}, threed, data);
            // this.root.updateLocaleWidget($.extend(true, {}, threed, data));
        }
    }
    queryThreedState(id) {
        let msg1 = "",
            msg2 = "";
        const threed = this.getThreedById(id);
        if (threed) {
            var TEXTS = this.consts.TEXTS;
            if (this.state == 0) {
                msg1 = TEXTS.GENERATION;
                msg2 = "";
                this.requestState && this.requestState(threed.md5);
            } else if (this.state == 1) {
                msg1 = TEXTS.GENERATED;
                // this.updateThreed(id, this.threedState);
            } else if (this.state == -1) {
                msg1 = TEXTS.GENERATE_ERROR;
                msg2 = TEXTS.CHECK_FILE;
            } else if (this.state == 2) {
                msg1 = TEXTS.GENERATED;
                msg2 = TEXTS.FILE_ERROR;
            }

            this.updateMsg(id, msg1, msg2)

            if (this.state === 0) {
                this.startLoop(threed);
            } else {
                this.remove(id);
            }
        }
    }
    startLoop(threed) {
        threed.timer = setTimeout(() => {
            this.queryThreedState(threed.id);
        }, this.interval);
    }
    destroy() {
        this.endAllLoop();
        this.threeds.clear();
    }
    judge(threed) {
        if (threed) {
            const exist = this.getThreedById(threed.id);
            if (exist) {
                this.endLoop(exist);
            }

            const local = $.extend(true, {}, threed);
            this.pushThreed(local);
            // zip格式
            if (!local.md5_android && !local.md5_ios) {
                this.queryThreedState(local.id);
                // unity3格式
            } else {
                this.updateMsg(local.id, this.consts.TEXTS.GENERATED, this.consts.TEXTS.PREVIEW);
            }
        }
    }
    inject({ consts, state, resetWidget, requestState } = {}) {
        this.consts = consts || this.consts;
        this.state = state;
        this.resetWidget = resetWidget || this.resetWidget;
        this.requestState = requestState || this.requestState;
    }
}

export default new ThreedHelper();
